//
//  Lotterys.m
//  K8彩票
//
//  Created by zhoubo on 2017/6/7.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "Lotterys.h"
#import "LotteryLayout.h"
#import "LotteryCollectionViewCell.h"
#import "HeaderView.h"
#import "LotteryModel.h"
#import "SliderView.h"

#define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]

#define randomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))

#import "KuaiVC.h"

@interface Lotterys ()<UICollectionViewDelegate,UICollectionViewDataSource>
//@property(nonatomic,strong)UIView* sliderView;

@property(nonatomic,strong) UICollectionView* collectionView;

@property (nonatomic,strong) HeaderView* headview;

// 用来控制一行显示几个元素
@property(nonatomic,assign)NSInteger num;

// 是否需要灰白相间(背景色)
@property(nonatomic,assign)BOOL isNeedAlternate;

// 一共显示多少数据
@property(nonatomic,assign)NSInteger count;

@property(nonatomic,strong)NSArray* models;


//@property (nonatomic,strong) NSTimer* countDownTimer;


@end



@implementation Lotterys

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setup];
        


        

        
        
    }
    return self;
}

-(void)setup
{
    if ([UIScreen mainScreen].bounds.size.width>500) {
        self.num = 3;
    }
    else
    {
        self.num = 2;
    }
    
    self.backgroundColor = [UIColor whiteColor];
    self.models = [LotteryModel getModels];
    HeaderView* headerView = [[HeaderView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.width*0.62 + 30)];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) collectionViewLayout:[[LotteryLayout alloc]initWithNum:self.num]];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[LotteryCollectionViewCell class] forCellWithReuseIdentifier:@"lotteryCell"];
    
    //注册头视图
    [self.collectionView registerClass:[headerView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headLottery"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.collectionView];
    
}




-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.models.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LotteryCollectionViewCell * cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"lotteryCell" forIndexPath:indexPath];
    
    cell.model = self.models[indexPath.row];
    cell.index = indexPath.row;
    
    cell.backgroundColor = [UIColor orangeColor];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LotteryCollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
    NSString* title = cell.model.title;
    
    if([title containsString:@"更多"])
    {
        return;
    }
    NSLog(@"----%@",title);
    LotteryKinds k = [LotteryModel getkindsWithTitle:title];
    switch (k) {
        case Fast3Lottery:
        {
            //创建一个消息对象
            NSString* info = nil;
            
            if ([title containsString:@"江苏"]) {
                info = @"jiangsufast3";
            }
            else if ([title containsString:@"湖北"])
            {
                info = @"hubeifast3";
            }
            else if ([title containsString:@"安徽"])
            {
                info = @"anhuifast3";
            }
            else
            {
                info = @"jiangsufast3";
            }
            
            NSNotification * notice = [NSNotification notificationWithName:@"fast3" object:nil userInfo:@{@"vc":info}];
            //发送消息
            [[NSNotificationCenter defaultCenter]postNotification:notice];
            break;
            
        }
            
        case Pailie3:
        {
            //创建一个消息对象
            NSNotification * notice = [NSNotification notificationWithName:@"fast3" object:nil userInfo:@{@"vc":@"pailie3"}];
            //发送消息
            [[NSNotificationCenter defaultCenter]postNotification:notice];
            break;
        }
        
        case Pailie5:
        {
            //创建一个消息对象
            NSNotification * notice = [NSNotification notificationWithName:@"fast3" object:nil userInfo:@{@"vc":@"pailie5"}];
            //发送消息
            [[NSNotificationCenter defaultCenter]postNotification:notice];
            break;
        }
            
        case Shuangse:
        {
            //创建一个消息对象
            NSNotification * notice = [NSNotification notificationWithName:@"fast3" object:nil userInfo:@{@"vc":@"shuangse"}];
            //发送消息
            [[NSNotificationCenter defaultCenter]postNotification:notice];
            break;
        }
        case Daletou:
        {
            //创建一个消息对象
            NSNotification * notice = [NSNotification notificationWithName:@"fast3" object:nil userInfo:@{@"vc":@"daletou"}];
            //发送消息
            [[NSNotificationCenter defaultCenter]postNotification:notice];
            break;
        }
        
    
 
            
        default:
        {
            //创建一个消息对象
            NSNotification * notice = [NSNotification notificationWithName:@"fast3" object:nil userInfo:@{@"vc":@"jiangsufast3"}];
            //发送消息
            [[NSNotificationCenter defaultCenter]postNotification:notice];
            break;
        }
        
    }
}


//  返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //如果是头视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headLottery" forIndexPath:indexPath];
        self.headview = header;

        return header;
    }
    //如果底部视图
    //    if([kind isEqualToString:UICollectionElementKindSectionFooter]){
    //
    //    }
    return nil;
}

-(void)setModel:(IssueModel *)model
{
    _model = model;
    self.headview.model = model;
    
}






//
//-(void)timeFireMethod{
//    //倒计时-1
//    //修改倒计时标签现实内容
////    self.timeLbl.text=[NSString stringWithFormat:@"%02d:%02d",self.secondsCountDown/60,self.secondsCountDown%60];
//    
//    for (LotteryModel* model in self.models) {
//        model.timeCount -= 1;
//        printf("---%ld",((LotteryModel*)self.models[0]).timeCount);
//        if (model.timeCount == 0) {
//            model.timeCount = model.totalCount;
//        }
//    }
//    printf("\n\n");
//    for (LotteryModel*model in self.models) {
//        NSLog(@"----%@",model.title);
//    }
//    [self.collectionView reloadData];
//    
//    
//    printf("----------------------------\n\n");
//    
//    
//
//}
//
////-(NSInteger)randomCount
////{
////    NSInteger count = (arc4random_uniform(2)+1)*300;
////    NSInteger interval = arc4random_uniform(100);
////    
////    return count-interval;
////}
//
//
//-(void)dealloc
//{
//    [self.countDownTimer invalidate];
//    self.countDownTimer = nil;
//}
//
//




@end
